package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.commons.ex.ServiceException;
import cn.tedu.csmall.commons.web.ServiceCode;
import cn.tedu.csmall.product.mapper.BrandCategoryMapper;


import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.mapper.SPUMapper;
import cn.tedu.csmall.product.pojo.entity.Brand;
import cn.tedu.csmall.product.pojo.entity.BrandCategory;
import cn.tedu.csmall.product.pojo.entity.SPU;
import cn.tedu.csmall.product.pojo.param.BrandAddNewParam;
import cn.tedu.csmall.product.pojo.param.BrandUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.commons.util.PageInfoToPageDataConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class BrandServiceImpl implements IBrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private BrandCategoryMapper brandCategoryMapper;

    @Autowired
    private SPUMapper spuMapper;

    @Override
    public void addNew(BrandAddNewParam brandAddNewParam) {
        log.debug("开始处理【添加品牌】的业务，参数：{}" ,brandAddNewParam);
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",brandAddNewParam.getName());
        int countByName = brandMapper.selectCount(queryWrapper);
        log.debug("根据品牌名称统计匹配的相册数量，结果：{}" , countByName);
        if (countByName>0){
            String message = "添加品牌失败,品牌名称已被占用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);

        }

        Brand brand = new Brand();
        BeanUtils.copyProperties(brandAddNewParam,brand);
        brand.setSales(0);
        brand.setProductCount(0);
        brand.setCommentCount(0);
        brand.setPositiveCommentCount(0);
        brand.setGmtCreate(LocalDateTime.now());
        brand.setGmtModified(LocalDateTime.now());
        log.debug("准备将新的品牌数据写入到数据库，数据：{}" , brand);
        int insert = brandMapper.insert(brand);

        if (insert != 1) {
            String message = "添加失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

        log.debug("将新的品牌数据写入到数据库，完成！");
    

    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【通过id删除品牌】的业务，参数：{}" ,id);
        //if (id<=0)在参数中配置此情况
        //检查品牌表中是否有此数据
        log.debug("检查品牌表中是否有此数据!");
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        int countByName = brandMapper.selectCount(queryWrapper);
        log.debug("根据品牌id统计匹配的品牌数量，结果：{}" , countByName);
        if (countByName==0){
            String message = "没有此id";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        //检查brand_category中有无数据和属性模板有关联
        log.debug("检查brand_category中有无数据和品牌有关联!");
        QueryWrapper<BrandCategory> queryWrapperBC = new QueryWrapper<>();
        queryWrapperBC.eq("brand_id",id);
        int countInBrandCategory = brandCategoryMapper.selectCount(queryWrapperBC);
        log.debug("根据品牌id统计匹配的品牌分类数量，结果：{}" , countInBrandCategory);
        if (countInBrandCategory != 0){
            String message = "存在品牌分类和品牌关联！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //检查spu中有无数据和属性模板有关联
        log.debug("检查spu中有无数据和品牌有关联!");
        QueryWrapper<SPU> queryWrapperSPU = new QueryWrapper<>();
        queryWrapperSPU.eq("brand_id",id);
        int countInSPU = spuMapper.selectCount(queryWrapperSPU);
        log.debug("根据品牌id统计匹配的品牌分类数量，结果：{}" , countInSPU);
        if (countInSPU != 0){
            String message = "存在spu和品牌关联！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        int i = brandMapper.deleteById(id);

        if (i != 1) {
            String message = "删除失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }

        log.debug("通过id删除属性模版数据，完成！");

    }


    @Override
    public void updateById(Long brandId, BrandUpdateInfoParam brandUpdateInfoParam) {

        log.debug("开始处理【通过id修改品牌】的业务，参数：{}" ,brandId);
        //if (id<=0)在参数中配置此情况
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",brandId);
        int countByName = brandMapper.selectCount(queryWrapper);
        log.debug("根据相册id统计匹配的相册数量，结果：{}" , countByName);
        if (countByName==0){
            String message = "没有此id号呀";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        Brand brand = brandMapper.selectById(brandId);
        brand.setName(brandUpdateInfoParam.getName());
        brand.setPinyin(brandUpdateInfoParam.getPinyin());
        brand.setLogo(brandUpdateInfoParam.getLogo());
        brand.setDescription(brandUpdateInfoParam.getDescription());
        brand.setKeywords(brandUpdateInfoParam.getKeywords());
        brand.setSort(brandUpdateInfoParam.getSort());

        int i = brandMapper.updateById(brand);

        if (i != 1) {
            String message = "修改失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }

        log.debug("通过id修改品牌数据，完成！");
    }

    @Override
    public BrandStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询品牌详情】的业务，参数：{}", id);
        BrandStandardVO queryResult = brandMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "根据id查询品牌详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        log.debug("即将返回品牌详情：{}", queryResult);
        return queryResult;
    }

    @Override
    public PageData<BrandListItemVO> list(Integer pageNum) {
        Integer pageSize = 5;
        return list(pageNum,pageSize);
    }

    @Override
    public PageData<BrandListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询品牌列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<BrandListItemVO> list = brandMapper.list();
        PageInfo<BrandListItemVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }
}
